package com.project.rfidpage.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.project.rfidpage.dto.BgDto;
import com.project.rfidpage.dto.BgParam;
import com.project.rfidpage.entity.Bg;
import com.project.rfidpage.mapper.BgMapper;
import com.project.rfidpage.service.BgService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BgServiceImpl implements BgService {

    @Resource
    private BgMapper bgMapper;

    @Override
    @DS("master")
    public List<BgDto> getListForPage(BgParam param) {
        return bgMapper.selectListByUid(param);
    }

    @Override
    @DS("master")
    public List<Bg> getByBgNo(String bgno) {
        Example example = new Example(Bg.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("bgno", bgno);
        return bgMapper.selectByExample(example);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void insert(Bg entity) {
        bgMapper.insert(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void update(Bg entity, String bgnoBefore) {
        Example example = new Example(Bg.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("bgno", bgnoBefore);
        bgMapper.updateByExample(entity, example);
    }
}
